﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace numer
{
    public partial class PlayGameScreen : Form
    {
        #region 変数
        // int in_num = 0;
        public string num1;
        public string num2;
        public string num3;

        public int targetNum1 = 0;
        public int targetNum2 = 0;
        public int targetNum3 = 0;

        private const string UPDATE_QUERY = @"DB.SQL.Update_Table.sql";

        private bool solve = false;

        //eat,byteの数(初期値0)
        private int eatnumber = 0;
        private int bytenumber = 0;

        private int winflag;

        #endregion

        #region 定数
        //各画面に割り振られている番号
        private const int MAINSCREEN = 1;
        private const int INPUTSCREEN = 2;
        private const int PLAYSCREEN = 3;
        private const int RESULTSCREEN_WIN = 4;
        private const int RESULTSCREEN_LOSE = 5;
        #endregion
        //　プロパティ
        private String number;
        /// <summary>
        /// 
        /// </summary>
        public String Number
        {
            get { return number; }
            set { number = value; }
        }


        public string parsonal;
        public string Parsonal
        {
            get { return parsonal; }
            set { parsonal = value; }
        }


        #region イベント処理

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public PlayGameScreen()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 画面ロード
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayGameScreen_Load(object sender, EventArgs e)
        {

            this.lblYourNum.Text = parsonal;

            //ひとり遊びよう M.Hamada ▼
            Random cRandom = new Random();
            
            this.targetNum1 = cRandom.Next(9);
            this.targetNum2 = cRandom.Next(9);
            this.targetNum3 = cRandom.Next(9);

            //ランダム処理修正 2015/1/23 M.FUkunishi ▼
            while (targetNum1 == targetNum2)
            {
                this.targetNum2 = cRandom.Next(9);
            }

            while (targetNum1 == targetNum3 || targetNum2 == targetNum3)
            {
                this.targetNum3 = cRandom.Next(9);
            }

            //string target;
            //target = targetNum1.ToString() + " " + targetNum2.ToString() + " " + targetNum3.ToString();
            //this.lblTargetNum.Text = target;
        }

        /// <summary>
        /// 0ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn0_Click(object sender, EventArgs e)
        {
            OutPanel(this.btn0.Text);
            //画面出力
            OutPanel(this.btn0.Text);
        }


        /// <summary>
        /// 1ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn1_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn1.Text);
        }


        /// <summary>
        /// 2ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn2_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn2.Text);
        }


        /// <summary>
        ///  3ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn3_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn3.Text);
        }


        /// <summary>
        /// 4ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn4_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn4.Text);
        }


        /// <summary>
        /// 5ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn5_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn5.Text);
        }


        /// <summary>
        /// 6ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn6_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn6.Text);
        }


        /// <summary>
        /// 7ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn7_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn7.Text);
        }


        /// <summary>
        /// 8ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn8_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn8.Text);
        }


        /// <summary>
        /// 9ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn9_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn9.Text);
        }
        
        /// <summary>
        /// 決定ボタンを押す
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnter_Click(object sender, EventArgs e)
        {

            bool startFlg = true;

            //EatByteJudge(num1, num2, num3);
            //EatByteJudge();

            //　自分の入力した数値とBIT数とBYTE数を表示///
            //lstResult.Items.Add(this.lblOutPanel1.Text + this.lblOutPanel2.Text + this.lblOutPanel3.Text + " x eat  y byte");

            num1 = lblOutPanel1.Text;
            num2 = lblOutPanel2.Text;
            num3 = lblOutPanel3.Text;

            int pnum1 = 0;
            int.TryParse(num1, out pnum1);
            int pnum2 = 0;
            int.TryParse(num2, out pnum2);
            int pnum3 = 0;
            int.TryParse(num3, out pnum3);

            //eat数を割り出す(サーバー処理)
            var client = new NumeR0nServer.Service1Client();
            //eat数を割り出す(サーバー処理)
            eatnumber = client.EatJudge(pnum1, pnum2, pnum3);
            //byte数を割り出す(サーバー処理)
            bytenumber = client.ByteJudge(pnum1, pnum2, pnum3);
            client.Close();

            lstResult.Items.Add(this.num1 + this.num2 + this.num3 + " " + eatnumber + "eat" + " " + bytenumber + "byte");
            this.lstResult.SelectedIndex = this.lstResult.Items.Count - 1;

            WinJudge();


            //  入力した数値を初期化
            this.lblOutPanel1.Text = "";
            this.lblOutPanel2.Text = "";
            this.lblOutPanel3.Text = "";

            //先攻後攻フラグを反転
            //DB.DBLogic.ExecuteNonQuery(UPDATE_QUERY);

            BtnActivate();
            //　決定ボタンを非表示
            //btnEnter.Enabled = false;

            //スレッドを生成する M.Hamada 2015/03/15
            Thread thread = new Thread(new ThreadStart(ThreadStart));
            thread.Start();

            //スレッドの終了
            thread.Join();

            //不成立の場合
            if(!startFlg)
            {
                //確立失敗メッセージ
                MessageBox.Show("タイムアウトになりました。\n スタート画面へ戻ります。");

                //初めの画面へ戻る
                //画面遷移
                Program.FormContainer.Change(MAINSCREEN);

                //TODO:もしテーブルレコード数が0でなければテーブルの初期化を行う
                //if()
                //{
                //    DB.DBLogic.ExecuteNonQuery(DaleteNonQuery);
                //}

                //処理の終了
                return;
            }

            BtnActivate();

        }

        /// <summary>
        /// クリアボタンクリックイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {

            //パネル③に値が入っていれば
            if (!string.IsNullOrEmpty(this.lblOutPanel3.Text))
            {
                //選択値をパネル③の値を消す
                this.lblOutPanel3.Text = "";

                // 決定ボタンを非表示
                btnEnter.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(this.lblOutPanel2.Text))
            {
                //選択値をパネル②の値を消す
                this.lblOutPanel2.Text = "";
            }
            else
            {
                //選択値をパネル①の値を消す
                this.lblOutPanel1.Text = "";
            }
        }
        #endregion

        #region その他の処理

        /// <summary>
        /// パネル表示処理
        /// </summary>
        /// <param name="number"></param>
        private void OutPanel(string number)
        {

            //入力値がパネル①またはパネル②と同じの場合
            if (number == this.lblOutPanel1.Text ||
                    number == this.lblOutPanel2.Text)
            {
                //処理終了
                return;
            }

            //パネル①が空白の場合
            if (string.IsNullOrEmpty(this.lblOutPanel1.Text))
            {
                //選択値をパネル①に出力する
                this.lblOutPanel1.Text = number;
            }
            //パネル①に入力値があり、パネル②が空白の場合
            else if (string.IsNullOrEmpty(this.lblOutPanel2.Text))
            {
                //選択値をパネル②に出力する
                this.lblOutPanel2.Text = number;
            }
            //パネル①およびパネル②に値が入っているかつパネル③の場合
            else if (string.IsNullOrEmpty(this.lblOutPanel3.Text))
            {
                //選択値をパネル③に出力する
                this.lblOutPanel3.Text = number;

                //決定ボタンを表示
                btnEnter.Enabled = true;
            }
            //パネルに3つ数字が入っているときは入力しない
            else
            {
                return;
            }

        }

        /// <summary>
        /// eat,byteの数を判定
        /// </summary>
        private void EatByteJudge()
        {

            //ひとりゲーム用乱数
            //Random cRandom = new System.Random();

            num1 = lblOutPanel1.Text;
            num2 = lblOutPanel2.Text;
            num3 = lblOutPanel3.Text;

            //仮の定数(相手の設定した3つの数字)
            //int NUM1 = cRandom.Next(9);
            //int NUM2 = cRandom.Next(9);
            //int NUM3 = cRandom.Next(9);

            //プレイヤーが宣言した数
            int pnum1 = 0;
            int.TryParse(num1, out pnum1);
            int pnum2 = 0;
            int.TryParse(num2, out pnum2);
            int pnum3 = 0;
            int.TryParse(num3, out pnum3);

            //プレイヤー、相手の数を格納する
            int[] player = new int[3] { pnum1, pnum2, pnum3 };
            int[] enemy = new int[3] { targetNum1, targetNum2, targetNum3 };

            //eatbyte判定用
            int i = 0;
            int j = 0;

            for (i = 0; i < 3; i++)
            {
                //数値かつ場所が合っていれば、eat+1
                if (player[i].Equals(enemy[i]))
                {
                    eatnumber++;
                }

                for (j = 0; j < 3; j++)
                {
                    //数値が合っていれば、byte+1(eatも含まれている)
                    if (player[j].Equals(enemy[i]))
                    {
                        bytenumber++;
                    }

                }

            }

            bytenumber = bytenumber - eatnumber;

            //ラベルにeat、byteの数を表示させる
            //this.eatlbl.Text = "eat:" + eatnumber;
            //this.bytelbl.Text = "byte:" + bytenumber;

            lstResult.Items.Add(this.lblOutPanel1.Text + this.lblOutPanel2.Text + this.lblOutPanel3.Text + " " + eatnumber + "eat" + " "  + bytenumber + "byte");
            this.lstResult.SelectedIndex = this.lstResult.Items.Count - 1;

            if (eatnumber==3)
            {
                solve = true;
            }
            else
            {
                //正解以外ならsolveをfalseにする 2015/1/17 M.Fukunishi
                solve = false;
            }

        }
        //20141129_Fukunishi_E
        //20141128_Goda-E

        #endregion

        #region チェック処理

        /// <summary>
        /// 入力結果判別処理
        /// </summary>
        /// <returns></returns>
        private bool checkresult()
        {
            return true;
        }

        #endregion

        /// <summary>
        /// 画面表示が変更されるとリストアイテムを削除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayGameScreen_VisibleChanged(object sender, EventArgs e)
        {
            //リストアイテム削除 2015/1/17 M.Fukunishi
            lstResult.Items.Clear();

            //lblYourNum.Text = personal[0];

        }



        /// <summary>
        /// スレッド処理
        /// </summary>
        private void ThreadStart()
        {

            System.Threading.Thread.Sleep(3000);

        }

        /// <summary>
        /// ボタン非活性処理
        /// </summary>
        public void BtnActivate()
        {

            if (this.btnEnter.Enabled)
            {
                this.btn0.Enabled = false;
                this.btn1.Enabled = false;
                this.btn2.Enabled = false;
                this.btn3.Enabled = false;
                this.btn4.Enabled = false;
                this.btn5.Enabled = false;
                this.btn6.Enabled = false;
                this.btn7.Enabled = false;
                this.btn8.Enabled = false;
                this.btn9.Enabled = false;
                this.btnClear.Enabled = false;
                this.btnEnter.Enabled = false;
            }
            else
            {

                this.btn0.Enabled = true;
                this.btn1.Enabled = true;
                this.btn2.Enabled = true;
                this.btn3.Enabled = true;
                this.btn4.Enabled = true;
                this.btn5.Enabled = true;
                this.btn6.Enabled = true;
                this.btn7.Enabled = true;
                this.btn8.Enabled = true;
                this.btn9.Enabled = true;
                this.btnClear.Enabled = true;

            }

        }

        /// <summary>
        /// 勝敗判別
        /// </summary>
        public void WinJudge()
        {
            if (eatnumber==3)
            {
                TurnJudge();
            }

        }

        /// <summary>
        /// 手番判別
        /// </summary>
        public void TurnJudge()
        {
            //winflagを参照

            //winflag 1:自分のターン 0:相手のターン
            if (winflag==1)
            {
                //画面遷移
                Program.FormContainer.Change(RESULTSCREEN_WIN);
            }
            else
            {
                //画面遷移
                Program.FormContainer.Change(RESULTSCREEN_LOSE);
            }
        }

    }
}
